﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HZ.BidSystem.IBLL;

using HZ.BidSystem.Model;
using HZ.Data;
using HZ.Common;
using System.Data;
using System.Net;


namespace HZ.BidSystem.BLL
{
    public class BLL_Company : IBLL_Company
    {
        #region IBLL_Company Members

        int IBLL_Company.BidPrice(IList<int> bidids, IList<double> prices, IList<int> Azone, IList<int> Bzone, IList<int> Czone, IList<int> Dzone, IList<int> Ezone)
        {
            ThrowHelper.Throw(bidids != null && prices != null);

            ThrowHelper.Throw(bidids.Count == prices.Count);
            // 调用加密工厂开始加密

            HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;

            var config = HZ.BidSystem.Model.SystemConfig.Instance;
            IList<string> encryptprices = new List<string>(prices.Count);
            for (int i = 0; i < prices.Count; i++)
            {
                // encryptprices.Add(prices[i].ToString());



                encryptprices.Add(ie.EncryptString(prices[i].ToString(), BLL_Cache.BidPwd));

            }


            var result = DAL.DAL_Project_Bid.BidPrice(bidids, encryptprices, Azone, Bzone, Czone,Dzone,Ezone);
            //为了防止大数据提交，导出死锁问题，现不使用表STAT_BidStatus by yjx 2016年8月15日 14:11:23
            //((IBLL_Company)this).RefreshStatistic(CurrentAccount.Account.AccountId.Value);
            return result;
        }

        int IBLL_Company.DecPrice(int companyid)
        {
            throw new NotImplementedException();
        }

        IList<View_BidPrice> IBLL_Company.GetBidPrice(HZ.BidSystem.Model.QBidPrice where, int currentpage, int pagesize, out int totalrecord)
        {
            var bidprice = DAL_Helper.GetPagedModel<View_BidPrice>(where, currentpage, pagesize, out totalrecord);
            foreach (var item in bidprice)
            {
                item.EncryptPrice = string.Empty;
                item.DecPrice = null;

            }
            return bidprice;
        }
        /// <summary>
        /// 获取报价产品列表，包含报价密文。
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pagesize">每页记录数</param>
        /// <param name="totalrecord">记录总数</param>
        /// <returns>数据源</returns>
        IList<View_BidPrice> IBLL_Company.GetBidPriceWithDec(object where, int currentpage, int pagesize, out  int totalrecord, string pwd1, string pwd2)
        {
            HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;
            var bidprice = DAL_Helper.GetPagedModel<View_BidPrice>(where, currentpage, pagesize, out totalrecord);

            var config = HZ.BidSystem.Model.SystemConfig.Instance;
            foreach (var item in bidprice)
            {
                if (item.DecPrice != null)
                {
                    continue;
                }
                if (!String.IsNullOrEmpty(item.EncryptPrice))
                {

                    item.DecPrice = new HZ.Common.ConvertHelper(ie.DecryptString(item.EncryptPrice, pwd1)).ToDecimal;
                    item.DecPrice2 = new HZ.Common.ConvertHelper(ie.DecryptString(item.EncryptPrice, pwd1)).ToDecimal / item.PackFactor;
                }



            }
            return bidprice;
        }

        /// <summary>
        /// 设置报价密码
        /// </summary>
        /// <param name="bidpwd">用户设置的报价密码</param>
        /// <param name="randpwd">随机密码</param>
        /// <returns>1成功 0 失败</returns>
        int IBLL_Company.SetBidPwd(string bidpwd, string randpwd, Int32 companyid, Int32 projectid)
        {
            var cps = new Company_Project_Switch() { ProjectId = projectid, CompanyId = companyid };
            cps = HZ.Data.DAL_Helper.GetModelByID(cps);

            var bidpwdstr = HZ.Fun.GetMd5(bidpwd + "_" + randpwd);
            Company_Project_Switch temp = new Company_Project_Switch() { ProjectId = projectid, CompanyId = companyid, BidPassword = bidpwdstr };
            return HZ.Data.DAL_Helper.OperateTableSQL(temp, TableOperator.Update);

        }

        /// <summary>
        /// 验证报价密码
        /// </summary>
        /// <param name="bidpwd"></param>
        /// <param name="randpwd"></param>
        /// <param name="CompanyId"></param>
        /// <param name="projectid"></param>
        /// <returns>1 成功 0 失败</returns>
        int IBLL_Company.CheckBidPwd(string bidpwd, string randpwd, int CompanyId, int projectid)
        {
            var cps = new Company_Project_Switch() { ProjectId = projectid, CompanyId = CompanyId };
            cps = HZ.Data.DAL_Helper.GetModelByID(cps);

            if (cps.BidPassword == HZ.Fun.GetMd5(bidpwd + "_" + randpwd))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }


        Company_Project_Switch IBLL_Company.GetCompanySwitch(int CompanyId, int ProjectId)
        {
            return HZ.Data.DAL_Helper.GetModelByID<Company_Project_Switch>(new Company_Project_Switch() { CompanyId = CompanyId, ProjectId = ProjectId });

        }


        /// <summary>
        /// 刷新企业统计缓存。
        /// </summary>
        /// <param name="companyID"></param>
        /// <returns></returns>
        int IBLL_Company.RefreshStatistic(Int32 companyID)
        {
            return DAL.DAL_BidStatus.RefreshStatistic(companyID);
        }



        int IBLL_Company.ConfirmBid(int AccountId, Int32 ProjectId)
        {
            var c = new Company_Project_Switch() { CompanyId = AccountId, ProjectId = ProjectId,ConfirmTime=DateTime.Now, ConfirmStatus = true };
            return HZ.Data.DAL_Helper.OperateTableSQL(c, TableOperator.Update);
        }

        int IBLL_Company.ConfirmBid2(int AccountId, Int32 ProjectId)
        {
            var d = new STAT_BidStatus() { CompanyId = AccountId, ConfirmStatus = true };
            return HZ.Data.DAL_Helper.OperateTableSQL(d, TableOperator.Update);
        }

        int IBLL_Company.DecPrice(int? CompanyId, int projectid, string pwd1, string pwd2, BidType BidType)
        {
            try
            {

                int i = 0;

                if (HZ.BidSystem.Model.SystemConfig.Instance.EncryptType == 0)
                {
                    switch (BidType)
                    {
                        case BidType.Bid:
                            i = DAL.DAL_Project_Bid.DecPrice(CompanyId, projectid, pwd1);
                            break;
                        case BidType.Bargain:
                            i = DAL.DAL_Bargain.DecBargain(CompanyId, projectid, pwd1);
                            break;
                        case BidType.Compete:
                            i = DAL.DAL_Compete.DecCompete(CompanyId, projectid, pwd1, CurrentAccount.CurrentCompeteTimes);
                            break;

                    }
                }
                else
                {
                    i = 1;
                }

                if (i>0)
                {
                    Company_Project_Switch cps = new Company_Project_Switch() { CompanyId = CompanyId, ProjectId = projectid, DecStatus = true, DecTime = DateTime.Now };
                    HZ.Data.DAL_Helper.OperateTableSQL(cps, TableOperator.Update);
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ee)
            {
                log4netManager.LogDebug("解密报价", string.Format("解密发生错误：操作用户名：{0}，操作IP：{1}", CurrentAccount.Account.AccountName, HZ.Fun.GetClientIP()), ee);

                throw;

                // 解密报价
                return 0;

            }

        }

        /// <summary>
        /// 获取议价信息
        /// </summary>
        /// <param name="Net"></param>
        /// <returns></returns>
        public DataTable getBarinPrice(string cg, string CompanyID)
        {
            return DAL.DAL_Bargain.getBarinPrice(cg, CompanyID);
        }

        /// <summary>
        /// 不显示上限价
        /// </summary>
        /// <param name="Net"></param>
        /// <returns></returns>
        public DataTable getNoBarinPrice(string cg )
        {
            return DAL.DAL_Bargain.getNoBarinPrice(cg);
        }


        /// <summary>
        /// 查看议价参考价
        /// </summary>
        /// <param name="Net"></param>
        /// <returns></returns>
        public DataTable GetMinprie(string cg) 
        {
            return DAL.DAL_Bargain.GetMinprie(cg);
        }

        /// <summary>
        /// 企业确认议价数据是否进入议价流程
        /// </summary>
        /// <param name="objquery"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable GetGoodsBarin(object objquery, int currentPage, int pageSize, out int totalRecord, string pwd1, string pwd2)
        {
            return DAL.DAL_Bargain.GetGoodsBarin(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 企业：获取专家第一轮指导价
        /// </summary> 
        public DataTable GetBarinBidPrice(object objquery, int currentPage, int pageSize, out int totalRecord, string pwd1, string pwd2)
        {
            return DAL.DAL_Bargain.GetBarinBidPrice(objquery, currentPage, pageSize, out totalRecord);
        }
        /// <summary>
        ///  企业：获取专家第二轮指导价
        /// </summary> 
        public DataTable GetBarinBidPrice2(object objquery, int currentPage, int pageSize, out int totalRecord, string pwd1, string pwd2)
        {
            return DAL.DAL_Bargain.GetBarinBidPrice2(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 议价产品确认
        /// </summary>
        /// <param name="Net"></param>
        /// <returns></returns>
        public int UpdateBarinPrice(HZ.BidSystem.Model.View.View_BargainPrice2 barin)
        {
            return DAL.DAL_Bargain.UpdateBarinPrice(barin);
        }

        /// <summary>
        /// 是否确认议价产品进行议价
        /// </summary>
        /// <param name="Net"></param>
        /// <returns></returns>
        public int UpdateGoodsBarin(HZ.BidSystem.Model.View.View_BargainPrice2 barin)
        {
            return DAL.DAL_Bargain.UpdateGoodsBarin(barin);
        }

        public int ExpertPriceSubmit(string ExpertID, int ExpertPriceNum)
        {
            return DAL.DAL_Bargain.ExpertPriceSubmit(ExpertID, ExpertPriceNum);

        }

        /// <summary>
        /// 议价产品第二轮确认
        /// </summary>
        /// <param name="Net"></param>
        /// <returns></returns>
        public int UpdateBarinPrice2(HZ.BidSystem.Model.View.View_BargainPrice2 barin)
        {
            return DAL.DAL_Bargain.UpdateBarinPrice2(barin);
        }

        /// <summary>
        /// 获取专家给价格数据
        /// </summary> 
        public DataTable GetExpertPrice(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPrice(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 获取专家给价格数据---仅供导出
        /// </summary> 
        public DataTable GetExpertPriceZJ(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceZJ(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 获取专家给价格数据---仅供导出
        /// </summary> 
        public DataTable GetExpertPriceZJ2(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceZJ2(objquery, currentPage, pageSize, out totalRecord);
        }

        //相似产品上限价换算
        public DataTable GetLikecg(string cg)
        {
            return DAL.DAL_Bargain.GetLikecg(cg);
        }
        //同招标单元拟中标价 
        public DataTable GetSortGoodsPrice(string sortid, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetSortGoodsPrice(sortid, currentPage, pageSize, out totalRecord);
        }
        /// <summary>
        /// 获取专家给价格数据
        /// </summary>
        /// <param name="objquery"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable GetExpertPrice2(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPrice2(objquery, currentPage, pageSize, out totalRecord);
        }

        public int UpdateBargainPriceBatch(System.Data.DataTable PriceTable, int ExpertPriceNum)
        {
            return DAL.DAL_Bargain.UpdateBargainPriceBatch(PriceTable, ExpertPriceNum);
        }



        public int UpdateBarinPrice2Argee(int ExpertState, string Company_Goods, string UserID)
        {
            return DAL.DAL_Bargain.UpdateBarinPrice2Argee(ExpertState, Company_Goods, UserID);
        }

        /// <summary>
        /// 中心查看专家议价情况
        /// </summary>
        /// <param name="objquery"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable GetBarginListPrice(string objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetBarginListPrice(objquery, currentPage, pageSize, out totalRecord);
        }


        /// <summary>
        /// 专家给价进成查看
        /// </summary> 
        public DataTable GetExpertPriceIng(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceIng(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 最终议价状态
        /// </summary> 
        public DataTable GetExpertPriceEnd(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceEnd(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 最终议价状态----导出用
        /// </summary> 
        public DataTable GetExpertPriceEndExport(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceEndExport(objquery, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 最终议价状态2
        /// </summary> 
        public DataTable GetExpertPriceEnd2(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceEnd2(objquery, currentPage, pageSize, out totalRecord);
        }


        /// <summary>
        /// 最终议价状态2---导出
        /// </summary> 
        public DataTable GetExpertPriceEndExport2(object objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetExpertPriceEndExport2(objquery, currentPage, pageSize, out totalRecord);
        }
        /// <summary>
        /// 中心查看专家议价情况  第二轮
        /// </summary> 
        public DataTable GetBarginListPrice2(string objquery, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL.DAL_Bargain.GetBarginListPrice2(objquery, currentPage, pageSize, out totalRecord);
        }

        IList<View_BargainPrice> IBLL_Company.GetBargainWithDec(object where, int currentpage, int pagessize, out int totalrecord, string pwd1, string pwd2)
        {
            // HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;
            var barginprice = DAL_Helper.GetPagedModel<View_BargainPrice>(where, currentpage, pagessize, out totalrecord);
            //foreach (var item in barginprice)
            //{
            //    if (!String.IsNullOrEmpty(item.BargainEncryptPrice))
            //    {
            //        //if (SystemConfig.Instance.EnableEncrypt)
            //        //{
            //        //    item.BargainDecPrice = new HZ.Common.ConvertHelper(ie.DecryptString(item.BargainEncryptPrice, pwd1)).ToDecimal;
            //        //}
            //        //else
            //        //{
            //        item.BargainDecPrice = new HZ.Common.ConvertHelper(item.BargainEncryptPrice).ToDecimal;

            //        //}

            //    }


            //}
            return barginprice;
        }

        IList<View_CompetePrice> IBLL_Company.GetCompeteWithDec(object where, int currentpage, int pagessize, out int totalrecord, string pwd1, string pwd2)
        {
            HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;
            var competeprice = DAL_Helper.GetPagedModel<View_CompetePrice>(where, currentpage, pagessize, out totalrecord);
            foreach (var item in competeprice)
            {
                if (!String.IsNullOrEmpty(item.CompeteEncryptPrice))
                {

                    item.CompeteDecPrice = new HZ.Common.ConvertHelper(ie.DecryptString(item.CompeteEncryptPrice, pwd1)).ToDecimal;

                }
            }
            return competeprice;
        }

        #endregion

        #region IBLL_Company Members

        /// <summary>
        /// 企业议价
        /// </summary>
        /// <param name="bidids">产品编号</param>
        /// <param name="prices">企业输入议价价格</param>
        /// <param name="bidpricesshuoming">价格说明</param>
        /// <returns></returns>
        int IBLL_Company.BargainPrice(IList<int> bidids, IList<double> prices, IList<string> bidpricesshuoming)
        {
            ThrowHelper.Throw(bidids != null && prices != null);

            ThrowHelper.Throw(bidids.Count == prices.Count);
            // 调用加密工厂开始加密

            // HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;

            var config = SystemConfig.Instance;

            IList<string> encryptprices = new List<string>(prices.Count);
            for (int i = 0; i < prices.Count; i++)
            {
                // 议价不加密

                encryptprices.Add(prices[i].ToString());

            }
            return DAL.DAL_Bargain.BargainPrice(bidids, encryptprices,bidpricesshuoming);
        }

        int IBLL_Company.BargainPriceCheck(IList<int> bidids, IList<int> bidcheck)
        {
            ThrowHelper.Throw(bidids != null && bidcheck != null);

            ThrowHelper.Throw(bidids.Count == bidcheck.Count);
            // 调用加密工厂开始加密

            return DAL.DAL_Bargain.BargainPriceCheck(bidids, bidcheck);
        }

        int IBLL_Company.CompetePrice(IList<int> bidids, IList<double> prices, int competetime)
        {
            ThrowHelper.Throw(bidids != null && prices != null);

            ThrowHelper.Throw(bidids.Count == prices.Count);
            // 调用加密工厂开始加密

            HZ.Security.IEncrypt ie = HZ.Security.EncryptFactory.Instance;


            var config = SystemConfig.Instance;
            IList<string> encryptprices = new List<string>(prices.Count);
            for (int i = 0; i < prices.Count; i++)
            {

                encryptprices.Add(ie.EncryptString(prices[i].ToString(), BLL_Cache.BidPwd));

            }


            return DAL.DAL_Compete.CompetePrice(bidids, encryptprices, competetime);
        }

        #endregion

        #region IBLL_Company Members


        System.Data.DataTable IBLL_Company.GetBidCountByUserCode(string usercode)
        {
            return DAL.DAL_BidStatus.GetBidCountByUserCode(usercode);
        }

        #endregion

        #region IBLL_Company Members


        int IBLL_Company.GetNoBidCount()
        {
            return DAL.DAL_Project_Bid.GetNoBidCount(CurrentAccount.Account.AccountId.Value);
        }

        #endregion
    }
}
